<!--
 * @description: 推演控制
 * @fileName: control.vue
 * @author: ssr
 * @date: 2022-08-11 15:06:10
 * @version: V1.0.0
!-->
<template>
  <div class="deduct-control">
    <!-- <a v-if="simulStore.isPause" class="btn" @click="handleStart">
      <i class="iconfont icon-jiediankaishi"></i>
      <span>{{ simulStore.isStart ? "继续" : "开始" }}</span>
    </a>
    <div v-else class="btn" @click="handlePause">
      <i class="iconfont icon-zanting"></i>
      <span>暂停</span>
    </div>
    <a class="btn" @click="handleRestart">
      <i class="iconfont icon-restart"></i>
      <span>重新开始</span>
    </a> -->
    <div
      class="btn"
      :class="{ active: simulStore.currentPerspective === 'all' }"
      @click="handleChangePerspective('all')"
    >
      <i class="iconfont icon-direct-view"></i>
      <span>导演视角</span>
    </div>
    <div
      class="btn"
      :class="{ active: simulStore.currentPerspective === 'red' }"
      @click="handleChangePerspective('red')"
    >
      <i class="iconfont icon-team-view"></i>
      <span>红方视角</span>
    </div>
    <div
      class="btn"
      :class="{ active: simulStore.currentPerspective === 'blue' }"
      @click="handleChangePerspective('blue')"
    >
      <i class="iconfont icon-team-view"></i>
      <span>蓝方视角</span>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, toRefs, watch } from "vue"
import {
  simulationState,
  appState,
} from "@/pinia/stores/index";

// 发出事件
const emit = defineEmits(['play', 'pause', 'restart', 'speedChange', 'perspectiveChange']);

// 导入store和路由
const appStore = appState();
const simulStore = simulationState();

// 响应式状态
const state = reactive<{ multiplier: number | string }>({
  multiplier: simulStore.getSimulSpeed.toFixed(1),
});

// 切换视角
const handleChangePerspective = (view: string) => {
  simulStore.currentPerspective = view;

  // 发出视角变化事件，让父组件处理数据更新
  emit('perspectiveChange', view);
};

// 重新开始 - 直接发出restart事件
const handleRestart = () => {
  // 更新UI状态
  simulStore.setIsPause(true);
  simulStore.isStart = false;
  simulStore.simulTime = "00:00:00";

  // 显示加载状态
  appStore.spinning = true;
  appStore.spinningText = {
    change: true,
    text: "加载数据中，请稍后......",
  };

  // 发出重启事件给父组件
  emit('restart');
};

// 开始播放 - 直接发出play事件
const handleStart = () => {
  simulStore.setIsPause(false);
  simulStore.isStart = true;

  // 发出播放事件给父组件
  emit('play');
};

// 暂停播放 - 直接发出pause事件
const handlePause = () => {
  simulStore.setIsPause(true);

  // 发出暂停事件给父组件
  emit('pause');
};

// 监听simulSpeed变化
watch(
  () => simulStore.simulSpeed,
  (newVal) => {
    state.multiplier = Number(newVal).toFixed(1);
  },
  {
    immediate: true,
  }
);

// 暴露属性和方法
const { multiplier } = toRefs(state);
</script>

<style lang="less">
.deduct-control {
  width: 100%;
  position: absolute;
  top: 40px;
  z-index: 1000;
  color: white;
  text-align: center;
  display: flex;
  flex-direction: row;
  justify-content: center;
  .btn {
    height: 40px;
    border-width: 1px;
    border-style: solid;
    border-color: #2c3649;
    border-left: none;
    background-color: #050f22;
    padding: 0 10px;
    display: flex;
    align-items: center;
    color: white;
    cursor: pointer;
    &:first-child {
      border-left: 1px solid #2c3649;
    }
    > span {
      padding-left: 5px;
    }
    &.active {
      color: #267dff;
    }
  }
}
</style>

